package com.appfahrt.gui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

	private static final String TAG = ExpandableListAdapter.class.getSimpleName();

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	private Context context;

//	private String[] groups = { "Meine Fahrgesuche", "Meine Fahrten", "Historie" };
	private ArrayList<String> groups = null; //{ "Meine Fahrgesuche", "Meine Fahrten", "Historie" };
	

//	private String[][] children = {};
	
	private ArrayList<ArrayList<String>> children = null;

	


	public ExpandableListAdapter(Context pContext){
		this.context = pContext;
		ArrayList<String> gruppen = new ArrayList<String>();
		gruppen.add("Meine Fahrgesuche");
		gruppen.add("Meine Fahrten");
		gruppen.add("Historie");
		setGroups(gruppen);
		setChildren();
	}

	public void addItem(ArrayList<ArrayList<String>> pChilderen) {
		Log.e(TAG, "pChilderen.length:" + pChilderen.size());
		if (pChilderen.size() > 0) {
			for (int y = 0; y < pChilderen.size(); y++) {
				ArrayList<String> aChild = pChilderen.get(y);
				Log.e(TAG, "aGroup.length:" + aChild.size());
				if (aChild.size() > 0) {
//					List<String> child = new ArrayList<String>();
//					for (int x = 0; x < aChild.size(); x++) {
//						Log.e(TAG, "aGroup[x]:" + child.get(x));
//						child.add(child.get(x));
//					}
//					Log.e(TAG,"child.toArray():"+ child.toArray(new String[child.size()]));
//					children.set(y, child);
					Log.e(TAG,"children.set(y, aChild):[" + y+"] aChild["+ aChild.get(0)+"]");
					children.set(y, aChild);
				}
			}
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	// Return a child view. You can load your custom layout here.
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
//		TextView textView = getGenericView();
//		textView.setText(getChild(groupPosition, childPosition).toString());
//		textView.setTextColor(R.color.darkBlue);
//		return textView;

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.child_layout, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
		tv.setText(getChild(groupPosition, childPosition).toString());
		return convertView;

	}

	@Override
	public int getGroupCount() {
		return groups.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	// Return a group view. You can load your custom layout here.
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
//		TextView textView = getGenericView();
//		textView.setText(getGroup(groupPosition).toString());
//		textView.setTextColor(R.color.red);
//		return textView;
		
	      String group = getGroup(groupPosition).toString();
	        if (convertView == null) {
	            LayoutInflater infalInflater = (LayoutInflater) context
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            convertView = infalInflater.inflate(R.layout.group_layout, null);
	        }
	        TextView tv = (TextView) convertView.findViewById(R.id.welcomearea_tvGroup);
	        tv.setText(group);
	        return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

	public TextView getGenericView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 64);

		TextView textView = new TextView(this.context);
		textView.setLayoutParams(lp);
		// Center the text vertically
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		textView.setTextColor(R.color.green);
		// Set the text starting position
		textView.setPadding(36, 0, 0, 0);
		return textView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		int i = 0;
		try {
			i = children.get(groupPosition).size();

		} catch (Exception e) {
		}

		return i;
	}
	
	public ArrayList<String> getGroups() {
		return groups;
	}

	public void setGroups(ArrayList<String> groups) {
		this.groups = groups;
	}
	
	public ArrayList<ArrayList<String>> getChildren() {
		return children;
	}
	
	private void setChildren(){
		children = new ArrayList<ArrayList<String>> ();
		ArrayList<String> aPart1 =new ArrayList<String>();
		children.add(aPart1);
		aPart1 =new ArrayList<String>();
		children.add(aPart1);
		aPart1 =new ArrayList<String>();
		children.add(aPart1);
	}

}
